/* @(#)GSBargainDAO.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-4-20
 *   Jenny Li     2006-4-26  添加方法queryBargainByCustom
 *   Jenny Li     2006-5-29  添加方法queryBargainById
 *   Jenny Li     2006-6-22  修改方法queryMaxBgnWeek,添加只查询主合同类型
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.sql.Connection;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSBargainBean;
import com.hitrust.gs.bean.GSBargainChargeBean;
import com.hitrust.gs.bean.GSBargainModfiyBean;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSBargainAndChargeVO;
import com.hitrust.gs.vo.GSBargainQueryVO;

/**
 * 
 */
public class GSBargainDAO extends GSBaseDAO {
    
    /**
     * 在合同表GS_BARGAIN中新增一笔记录
     * @author owen
     * @param aGSBargainBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean addBargain(GSBargainBean aGSBargainBean) throws GSException {

        String tSql="INSERT INTO GS_BARGAIN (GS_BGN_ID,GS_BGN_SERIAL,GS_BGN_TYPE," +
                "GS_BGN_USERTYPE,GS_BGN_USERID,GS_BGN_CONTENT,GS_BGN_NUMBER,GS_BGN_FILENUMBER," +
                "GS_BGN_FILENAME,GS_BGN_DEPARTMENT,GS_BGN_SIGNUSER,GS_BGN_SIGNDATE,GS_BGN_ENDDATE," +
                "GS_BGN_TERM,GS_BGN_VOL,GS_BGN_MAINSERIAL,GS_BGN_RETURNEDDATE,GS_BGN_REMARK," +
                "GS_BGN_EFFECT,GS_BGN_RECORDID,GS_BGN_RECORDTIME,GS_MAIN_ECTYPE_NUM,GS_BGN_WEEK) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        Object[] tObject = new Object[23] ;
          tObject[0]=aGSBargainBean.getGs_bgn_id();
          tObject[1]=aGSBargainBean.getGs_bgn_serial();
          tObject[2]=aGSBargainBean.getGs_bgn_type();
          tObject[3]=aGSBargainBean.getGs_bgn_usertype();
          tObject[4]=aGSBargainBean.getGs_bgn_userid();
          tObject[5]=aGSBargainBean.getGs_bgn_content();
          tObject[6]=aGSBargainBean.getGs_bgn_number();
          tObject[7]=aGSBargainBean.getGs_bgn_filenumber();
          tObject[8]=aGSBargainBean.getGs_bgn_filename();
          tObject[9]=aGSBargainBean.getGs_bgn_department();
          tObject[10]=aGSBargainBean.getGs_bgn_signuser();
          tObject[11]=aGSBargainBean.getGs_bgn_signdate();
          tObject[12]=aGSBargainBean.getGs_bgn_enddate();
          tObject[13]=aGSBargainBean.getGs_bgn_term();
          tObject[14]=aGSBargainBean.getGs_bgn_vol();
          tObject[15]=aGSBargainBean.getGs_bgn_mainserial();
          tObject[16]=aGSBargainBean.getGs_bgn_returneddate();
          tObject[17]=aGSBargainBean.getGs_bgn_remark();
          tObject[18]="1";
          tObject[19]=aGSBargainBean.getGs_bgn_recordid();
          tObject[20]=aGSBargainBean.getGs_bgn_recordtime();
          tObject[21]=aGSBargainBean.getGs_main_ectype_num();
          tObject[22]=aGSBargainBean.getGs_bgn_week();
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在合同费用信息表GS_BARGAIN_CHARGE中新增一笔记录
     * @author owen
     * @param aGSBargainChargeBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean addBargainCharge(GSBargainChargeBean aGSBargainChargeBean) throws GSException {

        String tSql="INSERT INTO GS_BARGAIN_CHARGE (GS_BGC_ID,GS_BGC_BGN_ID,GS_BGC_TYPE," +
                "GS_BGC_CONTENT) VALUES(?,?,?,?)";

        Object[] tObject = new Object[4] ;
          tObject[0]=aGSBargainChargeBean.getGs_bgc_id();
          tObject[1]=aGSBargainChargeBean.getGs_bgc_bgn_id();
          tObject[2]=aGSBargainChargeBean.getGs_bgc_type();
          tObject[3]=aGSBargainChargeBean.getGs_bgc_content();
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 合同信息是否存在
     * @author owen
     * @param aBgnSerial
     * @return
     * @throws GSException
     */
    public boolean isExist(String aBgnSerial) throws GSException{
        String tSql = "select * from GS_BARGAIN where GS_BGN_SERIAL = ?";
        boolean tFlag = false;
        Object[] tObj = {aBgnSerial};
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql,tObj);
            if(tList.size() > 0){
                tFlag = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tFlag;
    }
    
    /**
     * 查询合同信息详细信�息
     * @param aBgnSerial           
     * @return tGSBargainBean                    
     * @throws GSException
     */
    public GSBargainBean queryBargain(String aBgnSerial) throws GSException{
        GSBargainBean tGSBargainBean = null;;
        String tSql = "select * from GS_BARGAIN where GS_BGN_ID=?";
        Object tObj[] = {aBgnSerial};
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql,tObj);
            if(tList.size() > 0){
                tGSBargainBean =(GSBargainBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tGSBargainBean;
    }
    
    /**
     * 查询合同信息的类型
     * @param aBgnSerial           
     * @return tGSBargainBean                    
     * @throws GSException
     */
    public String typeID(String aBgnSerial) throws GSException{
        String tType = null;
        GSBargainBean tGSBargainBean=new GSBargainBean();
        String tSql = "select * from GS_BARGAIN where GS_BGN_SERIAL=?";
        Object tObj[] = {aBgnSerial};
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql,tObj);
            if(tList.size() > 0){
                tGSBargainBean =(GSBargainBean)tList.get(0);
                tType=tGSBargainBean.getGs_bgn_type();
            }
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tType;
    }
    
    /**
     * 通过序号查询合同信息详细信�息
     * @param aBgnSerial           
     * @return tGSBargainBean                    
     * @throws GSException
     */
    public GSBargainBean queryBargainID(String aBgnMainSerial) throws GSException{
        GSBargainBean tGSBargainBean = null;;
        String tSql = "select * from GS_BARGAIN where GS_BGN_MAINSERIAL=?";
        Object tObj[] = {aBgnMainSerial};
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql,tObj);
            if(tList.size() > 0){
                tGSBargainBean =(GSBargainBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tGSBargainBean;
    }
    
    /**
     * 查询所有合同费用信息
     * @return
     * @throws GSException
     */
    public ArrayList queryBargainCharge(String aBgnSerial) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from GS_BARGAIN_CHARGE where GS_BGC_BGN_ID=? order by GS_BGC_ID";
        Object[] tObj = {aBgnSerial};
        try{
            tList = this.executeQuery(new GSBargainChargeBean(),tSql,tObj);
           
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 查询主合同中的副合同数量
     * @return
     * @throws GSException
     */
    public ArrayList queryMainBargain(String aBgnSerial) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from GS_BARGAIN where GS_BGN_MAINSERIAL=?";
        Object[] tObj = {aBgnSerial};
        try{
            tList = this.executeQuery(new GSBargainBean(),tSql,tObj);
           
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 综合查询合同信息
     * @return
     * @throws GSException
     */
    public ArrayList searchBargain(Connection aConn,GSBargainQueryVO aGSBargainQueryVO) throws GSException{
        ArrayList tList = new ArrayList();
        StringBuffer tSql = new StringBuffer("select * from GS_BARGAIN ");
        tSql.append("where GS_BGN_CONTENT like '%" + aGSBargainQueryVO.getGs_bgn_content()+ "%'");
        if(!aGSBargainQueryVO.getGs_bgn_department().equals("")){
            tSql.append(" and GS_BGN_DEPARTMENT like '%"+aGSBargainQueryVO.getGs_bgn_department()+"'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_signuser().equals("")){
            tSql.append(" and GS_BGN_SIGNUSER like '%"+aGSBargainQueryVO.getGs_bgn_signuser()+"%'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_userid().equals("")){
            tSql.append(" and GS_BGN_USERID like '%"+aGSBargainQueryVO.getGs_bgn_userid()+"%'"); 
        }
        
        if(!aGSBargainQueryVO.getGs_bgn_serial().equals("")){
            tSql.append(" and GS_BGN_SERIAL = '"+aGSBargainQueryVO.getGs_bgn_serial()+"'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_vol().equals("")){
            tSql.append(" and GS_BGN_VOL = '"+aGSBargainQueryVO.getGs_bgn_vol()+"'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_term().equals("")){
            tSql.append(" and GS_BGN_TERM = '"+aGSBargainQueryVO.getGs_bgn_term()+"'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_number().equals("")){
            tSql.append(" and GS_BGN_NUMBER = '"+aGSBargainQueryVO.getGs_bgn_number()+"'");
        }
        if(!aGSBargainQueryVO.getGs_bgn_filenumber().equals("")){
            tSql.append(" and GS_BGN_FILENUMBER = '"+aGSBargainQueryVO.getGs_bgn_filenumber()+"'");
        }
       if(!aGSBargainQueryVO.getGs_bgn_effect().equals("")){
            tSql.append(" and GS_BGN_EFFECT = '"+aGSBargainQueryVO.getGs_bgn_effect()+"'");
        }
       //签约日期     
       tSql.append(" and to_char(to_date(GS_BGN_SIGNDATE,'YYYY-MM-DD'),'YYYYMMDD') >='"
               + aGSBargainQueryVO.getStartsigndate() + "'");
       tSql.append(" and to_char(to_date(GS_BGN_SIGNDATE,'YYYY-MM-DD'),'YYYYMMDD') <='"
               + aGSBargainQueryVO.getEndsigndate() + "'");
       
       //归卷日期
       if(!(aGSBargainQueryVO.getStartreturneddate().equals("00000000")&&aGSBargainQueryVO.getEndreturneddate().equals("99999999"))){
         tSql.append(" and to_char(to_date(GS_BGN_RETURNEDDATE,'YYYY-MM-DD'),'YYYYMMDD') >='"
               + aGSBargainQueryVO.getStartreturneddate() + "'");
         tSql.append(" and to_char(to_date(GS_BGN_RETURNEDDATE,'YYYY-MM-DD'),'YYYYMMDD') <='"
               + aGSBargainQueryVO.getEndreturneddate() + "'");
       }
       //终止日期
       if(aGSBargainQueryVO.getOnenddate()!=null&&aGSBargainQueryVO.getOnenddate().equals("1")){
               tSql.append(" and to_char(to_date(GS_BGN_ENDDATE,'YYYY-MM-DD'),'YYYYMMDD') >='"
                   + aGSBargainQueryVO.getStartenddate() + "'");
               tSql.append(" and to_char(to_date(GS_BGN_ENDDATE,'YYYY-MM-DD'),'YYYYMMDD') <='"
                   + aGSBargainQueryVO.getStopenddate() + "'");
           }else{
               if(!(aGSBargainQueryVO.getStartenddate().equals("00000000")&&aGSBargainQueryVO.getStopenddate().equals("99999999"))){
                   tSql.append(" and to_char(to_date(GS_BGN_ENDDATE,'YYYY-MM-DD'),'YYYYMMDD') >='"
                       + aGSBargainQueryVO.getStartenddate() + "'");
                   tSql.append(" and to_char(to_date(GS_BGN_ENDDATE,'YYYY-MM-DD'),'YYYYMMDD') <='"
                       + aGSBargainQueryVO.getStopenddate() + "'");
           }
       }
       //以主、副合同合并方式显示    
       if(aGSBargainQueryVO.getMaintype()!=null&&aGSBargainQueryVO.getMaintype().equals("1")){
           tSql.append(" and GS_BGN_TYPE ='1'");
        }
        //排序
        tSql.append(" order by GS_MAIN_ECTYPE_NUM desc,GS_BGN_TYPE,GS_BGN_RECORDTIME desc");
        GSLogWriter.debug(this, tSql.toString());
        Object[] tObj = {};
        try{
            tList = this.executeQuery(new GSBargainBean(),tSql.toString(),tObj);
           
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 在合同费用信息表修改成为无效
     * @author owen
     * @param  aBgnEffect,aBgnSerial
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean modifyBargainEffect(String aBgnEffect,String aBgnSerial) throws GSException {

        String tSql=" UPDATE GS_BARGAIN SET GS_BGN_EFFECT = ? WHERE GS_BGN_ID = ?";

        Object[] tObject = new Object[2] ;
          tObject[0]=aBgnEffect;
          tObject[1]=aBgnSerial;
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在合同费用信息表把主合同下面的副合同修改成为无效
     * @author owen
     * @param  aBgnEffect,aBgnSerial
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean modifyLowerBargainEffect(String aBgnEffect,String aBgnSerial) throws GSException {

        String tSql=" UPDATE GS_BARGAIN SET GS_BGN_EFFECT = ? WHERE GS_BGN_MAINSERIAL = ?";

        Object[] tObject = new Object[2] ;
          tObject[0]=aBgnEffect;
          tObject[1]=aBgnSerial;
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在合同费用信息表中删除一笔记录
     * 
     * @author owen
     * 
     * @param aBgnSerial 合同编号
     *           
     */
    public boolean deleteBargainCharge(String aBgnSerial) throws GSException {
        String tSql = "delete from GS_BARGAIN_CHARGE where GS_BGC_BGN_ID=?";
        Object[] tObject = { aBgnSerial };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 根据费用序号合同费用信息表中删除一笔记录
     * 
     * @author owen
     * 
     * @param aBgnSerial 合同编号
     *           
     */
    public boolean deleteBargainChargeID(String aId) throws GSException {
        String tSql = "delete from GS_BARGAIN_CHARGE where GS_BGC_ID=?";
        Object[] tObject = { aId };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在合同信息表中删除一笔记录
     * 
     * @author owen
     * 
     * @param aBgnSerial 合同编号
     *           
     */
    public boolean deleteBargain(String aBgnSerial) throws GSException {
        String tSql = "delete from GS_BARGAIN where GS_BGN_ID=?";
        Object[] tObject = { aBgnSerial };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在合同信息表中删除主合同中的副合同
     * 
     * @author owen
     * 
     * @param aBgnSerial 合同编号
     *           
     */
    public boolean deleteLowerBargain(String aBgnSerial) throws GSException {
        String tSql = "delete from GS_BARGAIN where GS_BGN_MAINSERIAL=?";
        Object[] tObject = { aBgnSerial };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 修改合同信息
     * @author owen
     * @param  aGSBargainBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean modifyBargain(GSBargainBean aGSBargainBean,String aOldId) throws GSException {
        
        String tSql=" UPDATE GS_BARGAIN SET GS_BGN_USERTYPE = ?,GS_BGN_USERID=?,GS_BGN_CONTENT=?," +
                "GS_BGN_NUMBER=?,GS_BGN_FILENUMBER=?,GS_BGN_FILENAME=?,GS_BGN_DEPARTMENT=?," +
                "GS_BGN_SIGNUSER=?,GS_BGN_SIGNDATE=?,GS_BGN_ENDDATE=?,GS_BGN_TERM=?,GS_BGN_VOL=?," +
                "GS_BGN_RETURNEDDATE=?,GS_BGN_REMARK=?,GS_BGN_WEEK=?," +
                "GS_BGN_SERIAL = ?,GS_BGN_MAINSERIAL=? where GS_BGN_ID=?";

        Object[] tObject = new Object[18] ;
          tObject[0]=aGSBargainBean.getGs_bgn_usertype();
          tObject[1]=aGSBargainBean.getGs_bgn_userid();
          tObject[2]=aGSBargainBean.getGs_bgn_content();
          tObject[3]=aGSBargainBean.getGs_bgn_number();
          tObject[4]=aGSBargainBean.getGs_bgn_filenumber();          
          tObject[5]=aGSBargainBean.getGs_bgn_filename();
          tObject[6]=aGSBargainBean.getGs_bgn_department();
          tObject[7]=aGSBargainBean.getGs_bgn_signuser();
          tObject[8]=aGSBargainBean.getGs_bgn_signdate();
          tObject[9]=aGSBargainBean.getGs_bgn_enddate();
          tObject[10]=aGSBargainBean.getGs_bgn_term();
          tObject[11]=aGSBargainBean.getGs_bgn_vol();
          tObject[12]=aGSBargainBean.getGs_bgn_returneddate();
          tObject[13]=aGSBargainBean.getGs_bgn_remark();
          tObject[14]=aGSBargainBean.getGs_bgn_week();
          tObject[15]=aGSBargainBean.getGs_bgn_serial();
          tObject[16]=aGSBargainBean.getGs_bgn_mainserial();
          tObject[17]=aGSBargainBean.getGs_bgn_id();
          
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 修改副合同编号
     * @author owen
     * @param  aGSBargainChargeBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean modifyBargainId(String aMainSerial,String aId) throws GSException {
        
        String tSql=" UPDATE GS_BARGAIN SET GS_BGN_MAINSERIAL=? WHERE GS_BGN_MAINSERIAL=?";

          Object[] tObject = new Object[2] ;
          tObject[0]=aMainSerial;
          tObject[1]=aId;
          
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 修改合同费用信息
     * @author owen
     * @param  aGSBargainChargeBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean modifyBargainCharge(GSBargainChargeBean aGSBargainChargeBean) throws GSException {
        
        String tSql=" UPDATE GS_BARGAIN_CHARGE SET GS_BGC_TYPE = ?,GS_BGC_CONTENT=? " +
                "WHERE GS_BGC_BGN_ID = ? and GS_BGC_ID=?";

          Object[] tObject = new Object[4] ;
          tObject[0]=aGSBargainChargeBean.getGs_bgc_type();
          tObject[1]=aGSBargainChargeBean.getGs_bgc_content();
          tObject[2]=aGSBargainChargeBean.getGs_bgc_bgn_id();
          tObject[3]=aGSBargainChargeBean.getGs_bgc_id();
         
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    /**
     * 根据客户名称查询合同信息
     * @param aCustomType			客户类别
     * @param aCustomId				客户编码
     * @return
     * @throws GSException
     */
    public ArrayList queryBargainByCustom(String aCustomType,String aCustomId)throws GSException{
        ArrayList tList = new ArrayList();
        StringBuffer tSql = new StringBuffer();
        if(aCustomType.equals(GSBaseConstants.BALANCE_ORDERDEPT_TYPE)){
            tSql.append("select A.GS_BGN_ID as P1,A.GS_BGN_SERIAL as P2,A.GS_BGN_TYPE as P3,A.GS_BGN_CONTENT as P4 ");
//            tSql.append("B.GS_BGC_CONTENT as P5,B.GS_BGC_TYPE as P6 ");
            tSql.append("from GS_BARGAIN A,GS_ORGANIZATION C where A.GS_BGN_USERID=C.GS_ORG_NAME ");
            tSql.append("and C.GS_ORG_ID=? order by A.GS_BGN_MAINSERIAL,A.GS_BGN_SERIAL");
        }else if(aCustomType.equals(GSBaseConstants.BALANCE_PROVIDER_TYPE)){
            tSql.append("select A.GS_BGN_ID as P1,A.GS_BGN_SERIAL as P2,A.GS_BGN_TYPE as P3,A.GS_BGN_CONTENT as P4 ");
//            tSql.append("B.GS_BGC_CONTENT as P5,B.GS_BGC_TYPE as P6 ");
            tSql.append("from GS_BARGAIN A,GS_PROVIDER C where A.GS_BGN_USERID=C.GS_PROVIDER_NAME ");
            tSql.append("and C.GS_PROVIDER_ID=? order by A.GS_BGN_MAINSERIAL,A.GS_BGN_SERIAL");
        }
        Object[] tObj = {aCustomId};
        
        try{
            tList = this.executeQuery(new GSBargainAndChargeVO(),tSql.toString(),tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
            
        }
        return tList;
    }
    /**
     * 根据序号关键字查询合同
     * @param aBgnId        序号
     * @return
     * @throws GSException
     */
    public GSBargainBean queryBargainById(String aBgnId) throws GSException{
        GSBargainBean tBgnBean = null;
        String tSql = "select * from GS_BARGAIN where GS_BGN_ID='" + aBgnId + "'";
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql,null);
            if(tList.size() == 1){
                tBgnBean = (GSBargainBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tBgnBean;
    }
    
    /**
     * 在合同修改信息表GS_BARGAIN_MODFIY中新增一笔记录
     * @author owen
     * @param aGSBargainChargeBean
     * 
     * return boolean
     * 
     * @throws GSException
     */
    public boolean addBargainModfiy(GSBargainModfiyBean aGSBargainModfiyBean) throws GSException {

        String tSql="INSERT INTO GS_BARGAIN_MODFIY (GS_BGM_SERIAL,GS_BGM_ID,GS_BGM_USERID," +
                "GS_BGM_DATE,GS_BGM_CONTEXT) VALUES(?,?,?,?,?)";

        Object[] tObject = new Object[5] ;
          tObject[0]=aGSBargainModfiyBean.getGs_bgm_serial();
          tObject[1]=aGSBargainModfiyBean.getGs_bgm_id();
          tObject[2]=aGSBargainModfiyBean.getGs_bgm_userid();
          tObject[3]=aGSBargainModfiyBean.getGs_bgm_date();
          tObject[4]=aGSBargainModfiyBean.getGs_bgm_context();
          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 查询合同修改信息
     * @return
     * @throws GSException
     */
    public ArrayList queryBargainModfiy(String aBgmSerial) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from GS_BARGAIN_MODFIY where GS_BGM_SERIAL=? order by GS_BGM_DATE desc";
        Object[] tObj = {aBgmSerial};
        try{
            tList = this.executeQuery(new GSBargainModfiyBean(),tSql,tObj);
           
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 取得指定供应商的合同中最大付款周期
     * @param aProviderId
     * @return
     * @throws GSException
     */
    public String queryMaxBgnWeek(String aDeptId,String deptCategory) throws GSException{
        String tWeek = "";
        StringBuffer tSql = new StringBuffer();
        if(deptCategory.equals(GSBaseConstants.BALANCE_PROVIDER_TYPE)){
            tSql.append("select * from GS_BARGAIN A,GS_PROVIDER B ");
            tSql.append("where A.GS_BGN_USERID=B.GS_PROVIDER_SHORTNAME and B.GS_PROVIDER_ID='" + aDeptId +"'");
            tSql.append(" and A.GS_BGN_SIGNDATE=(select max(A.GS_BGN_SIGNDATE) from GS_BARGAIN A,GS_PROVIDER B");
            tSql.append(" where A.GS_BGN_USERID=B.GS_PROVIDER_NAME and B.GS_PROVIDER_ID='" + aDeptId +"' and A.GS_BGN_TYPE='1')");
            tSql.append(" and A.GS_BGN_TYPE='1'");
        }else if(deptCategory.equals(GSBaseConstants.BALANCE_ORDERDEPT_TYPE)){
            tSql.append("select * from GS_BARGAIN A,GS_ORGANIZATION B ");
            tSql.append("where A.GS_BGN_USERID=B.GS_ORG_NAME and B.GS_ORG_ID='" + aDeptId +"'");
            tSql.append(" and A.GS_BGN_SIGNDATE=(select max(A.GS_BGN_SIGNDATE) from GS_BARGAIN A,GS_ORGANIZATION B");
            tSql.append(" where A.GS_BGN_USERID=B.GS_ORG_NAME and B.GS_ORG_ID='" + aDeptId +"' and A.GS_BGN_TYPE='1')");
            tSql.append(" and A.GS_BGN_TYPE='1'");
        }
        GSLogWriter.debug(this,tSql.toString());
        try{
            ArrayList tList = this.executeQuery(new GSBargainBean(),tSql.toString(),null);
            if(tList.size() == 1){
                tWeek = ((GSBargainBean)tList.get(0)).getGs_bgn_week();
            }
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return tWeek;
    }
}
